#!/usr/bin/env bash

filename=$HOME/study/gitee/collection/wiki/note.md
function showinfo() {
    if [[ $2 == 1 ]]; then
        grep "$1:" $filename|glow
        unset filename
        exit
    fi
}
case $1 in
    "")
        cat $filename |glow;;
    h|-h|--help)
        h h note;;
    v)
        vim $filename;;
    s)
        showinfo note $#
        grep "$2" $filename|glow;;
    a)
        showinfo add $#
        shift
        echo -e "1. \`add:\`\t"$@ >> $filename;;
    r)
        showinfo rm $#
        shift
        echo -e "1. \`rm:\`\t"$@ >> $filename;;
    bk)
        showinfo bak $#
        shift
        echo -e "1. \`bak:\`\t"$@ >> $filename;;
    tt)
        showinfo todo $#
        shift
        echo -e "1. \`todo:\`\t"$@ >> $filename;;
    td)
        showinfo done $#
        shift
        echo -e "1. \`done:\`\t"$@ >> $filename;;
    tc)
        shift
        grep "$@" $filename |glow
        echo "请问是否要将todo改为done？[y/n]"
        read -r input
        if [[ $input == "y" ]];then
            sed -i "/$@/s/todo/done/" $filename
            echo "已完成替换。"
        fi
        unset input;;
    d)
        shift
        sed -n "$1 p" $filename|glow
        echo "请问是否要删除这条？[y/n]"
        read -r input
        if [[ $input == "y" ]];then
            sed -i "$1 d" $filename
            echo "已删除。"
        fi
        unset input;;
    *)
        echo -e "1. \`note:\`\t"$@ >> $filename;;
esac
unset filename

